package com.okonna.selenium.tide;

import java.io.File;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;
import org.eclipse.swt.widgets.Display;

import com.okonna.selenium.tide.managers.RCManager;

public class Application
{
    private RCManager selRCManager;
    private Configuration config = null;
    private Display display;
    private File cfgFile;    
    
    public Application()
    {
		System.out.println(this.getClass().getSimpleName() + ".Application()");
		selRCManager = RCManager.getInstance();
    }
    
    public RCManager getRCManager()
    {
		System.out.println(this.getClass().getSimpleName() + ".getRCManager()");
		return selRCManager;
    }
    
    public void startSession()
    {
		System.out.println(this.getClass().getSimpleName() + ".startSession()");
		try
		{
			selRCManager.startServerSession(this.getConfig());
		    selRCManager.startClientSession(this.getConfig());
		}
		catch (Exception e)
		{
		    System.out.println("\nException:\n" + e + "\n");
		}
    }
	    
    public void stopSession()
	{
	    System.out.println(this.getClass().getSimpleName() + ".stopSession()");
		try
		{
		    selRCManager.stopClientSession();
		    selRCManager.stopServerSession();
		}
		catch (Exception e)
		{
		    System.out.println("\nException:\n" + e + "\n");
		}
    }
    
    public void restartClientSession()
	{
	    System.out.println(this.getClass().getSimpleName() + ".stopSession()");
		try
		{
		    selRCManager.restartClientSession();
		}
		catch (Exception e)
		{
		    System.out.println("\nException:\n" + e + "\n");
		}
    }
    
    public Configuration getConfig()
    {
    	System.out.println(this.getClass().getSimpleName() + ".getConfiguration()");
    	if (config == null)
    	{
	    	try
	    	{
	    		cfgFile = FileUtils.getFile("application.properties");
		    	config = new PropertiesConfiguration(cfgFile);
			} 		
			catch (Exception e)
			{
			    System.out.println("\nException:\n" + e + "\n");
			}
    	}
    	return config;
    }
    
    public void setDisplay(Display tmpDisplay)
    {
    	display = tmpDisplay;
    	return;
    }
    
    public Display getDisplay()
    {
    	return display;
    }
}
